package com.mayikt.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.bean.MeiteBeanUtils;
import com.mayikt.constants.Constants;
import com.mayikt.generator.TokenGenerator;
import com.mayikt.member.entity.UserDo;
import com.mayikt.member.entity.UserTokenDo;
import com.mayikt.member.input.dto.UserTokenInDTO;
import com.mayikt.member.mapper.UserMapper;
import com.mayikt.member.mapper.UserTokenMapper;
import com.mayikt.member.service.MemberLoginService;
import com.mayikt.utils.Md5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname MemerLoginServiceImpl
 * @Description SpringBoot SpringCloud 构建微服务电商平台。
 * @Created by CHEN JIN LONG
 * @Date 2019/1/25 02:27
 * @Version 1.0
 */
@RestController
public class MemerLoginServiceImpl extends BaseApiService<JSONObject> implements MemberLoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenGenerator tokenGenerator;

    @Autowired
    private UserTokenMapper userTokenMapper;

    @Override
    public BaseResponse<JSONObject> login(@RequestBody UserTokenInDTO userTokenInDTO) {

        String mobile = userTokenInDTO.getMOBILE();
        String password = userTokenInDTO.getPASSWORD();
        String loginType = userTokenInDTO.getLoginType();
        String deviceInfo = userTokenInDTO.getDeviceInfo();
        //1.验证参数

        if (StringUtils.isEmpty(mobile))
            return setResultError("手机号不能为空");
        if (StringUtils.isEmpty(password))
            return setResultError("密码不能为空");
        //2.登录密码加密
        password = Md5Utils.encryptPassword(password, "mayikt");
        //3.使用手机号码加密码进行登录
        UserDo userDo = userMapper.login(mobile, password);
        if (userDo == null)
            return setResultError("用户名或密码错误");
        Long userId = userDo.getUSER_ID();
        //查询当前登录类型账号之前是否有登录过
        UserTokenDo userTokenDo = userTokenMapper.selectByUserIdAndLoginType(userId, loginType);

        if (userTokenDo != null) {
            String token = userTokenDo.getToken();

            //修改数据中已经登录token状态
            int res = userTokenMapper.updateAvailability(token);
            if (res > 0)
                //清除原有的redis
                tokenGenerator.removeToken(token);
        }
        String keyPrefix = Constants.MEMBER_TOKEN_KEYPREFIX;
        //生成令牌
        String newToken = tokenGenerator.createToke(keyPrefix + "_" + loginType, userId + "");

        //插入新的token
        UserTokenDo userToken = new UserTokenDo();
        userToken.setUserId(userId);
        userToken.setLoginType(loginType);
        userToken.setToken(newToken);
        userToken.setDeviceInfo(deviceInfo);
        userTokenMapper.insertUserToken(userToken);

        JSONObject data = new JSONObject();
        data.put("token", newToken);

        return setResultSuccess(data);
    }
}
